home *** CD-ROM | disk | FTP | other *** search
- Path: rain.fr!world-net!usenet
- From: Frederic LACHASSE <lachass@worldnet.fr>
- Newsgroups: comp.lang.c++
- Subject: Re: Explicit copy constructor calls illegal?
- Date: Sun, 17 Mar 1996 07:35:13 +0000
- Organization: World-Net information exchange, Internet provider.
- Message-ID: <VA.0000006a.0002829a@fred>
- References: <4i9tp8$fgt@reznor.larc.nasa.gov>
- Reply-To: lachass@worldnet.fr
- NNTP-Posting-Host: client122.sct.fr
- X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
-
- In article <4i9tp8$fgt@reznor.larc.nasa.gov>, lance@jitter.larc.nasa.gov
- (Michael Lance) wrote:
- >
- > Hey!
- >
- > I am not a C++ newbie, but am feeling like I missed something:
- >
- > Why can I not make an explicit call to a copy constructor from a class
- > member function? To define my terminology, the copy constructor is the
- > function:
- > MyClass(const MyClass&)
- >
- > When I try to do this (just trying to re-use code - it is there, why not?),
- > a new instance of the class is instantiated SOMEWHERE, but a cout in the
- > constructor does NOT reflect it. The copy constructor properly assigns
- > the member variables of the instance, and the destructor registers its
- > destruction!
- >
- > Any clues, or am I violating a basic tenet? Lippman 2nd. edition is no
- > help here.
- >
- There is a hack to call a constructor: use the placement new operator. It is
- defined as:
-
- inline void* operator new(size_t, void* ptr) { return ptr; }
-
- So you can call a constructor (for example a copy constructor) on an existing
- piece of memory:
-
- MyClass a;
- char space[sizeof(MyClass)];
- MyClass* pcopy = new(space) MyClass(a);
-
- Explicit calling of the destructor is allowed so:
-
- pcopy->~MyClass();
-
- will destroyed this object.
-
- Of course, that's dangerous pratice, so think twice before doing that.
-
- Frederic LACHASSE (ECP 86)
- CompuServe: 100530,2005
- Internet: lachass@worldnet.fr
-
-